package com.yupfeg.executorsample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.yupfeg.executor.ExecutorProvider
import com.yupfeg.executor.ext.buildDefCPUThreadPoolFactory
import com.yupfeg.executor.ext.ioThreadPoolBuilder
import com.yupfeg.executor.ext.prepareExecutor

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //初始化操作，只执行一次
        prepareExecutor {
            //创建默认cpu线程池的工厂类
            buildDefCPUThreadPoolFactory {
                //配置io密集型线程池
                ioThreadPoolBuilder {
                    //限制最大并发数
                    maxPoolSize = 30
                    keepAliveTime = 10
                }
            }
        }

        //默认配置的cpu密集型线程池
        ExecutorProvider.cpuExecutor.apply {
            for (i in 0 until 20){
                execute {
                    Thread.sleep(1000)
                    Log.i("logger","使用cpu线程池执行任务 $i , ${Thread.currentThread().name}")
                }
            }
        }
        //默认配置的IO密集型线程池，大量短时任务
        ExecutorProvider.ioExecutor.apply {
            for (i in 0 until 300){
                execute {
                    Thread.sleep(200)
                    Log.i("logger","执行io线程池执行任务 $i , ${Thread.currentThread().name}")
                }
            }
        }

    }
}